package com.ytjj.qmyx.admin.controller;

import com.ytjj.common.api.CommonPage;
import com.ytjj.common.api.CommonResult;
import com.ytjj.qmyx.admin.annotation.OperLog;
import com.ytjj.qmyx.admin.annotation.oplog.OperationLogger;
import com.ytjj.qmyx.admin.annotation.oplog.annotation.OperationLog;
import com.ytjj.qmyx.admin.constants.SysLogConstants;
import com.ytjj.qmyx.admin.dao.ProductCategoryDao;
import com.ytjj.qmyx.admin.model.ProductSpecialAddRequest;
import com.ytjj.qmyx.admin.model.request.*;
import com.ytjj.qmyx.admin.model.response.DynamicModeResponse;
import com.ytjj.qmyx.admin.model.response.SpecialProductResponse;
import com.ytjj.qmyx.admin.model.response.SpecialRecommendResponse;
import com.ytjj.qmyx.admin.model.response.SpecialResp;
import com.ytjj.qmyx.mall.model.*;
import com.ytjj.qmyx.admin.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 专区管理
 */
@RestController
@RequestMapping("/special")
public class SpecialController {

    @Autowired
    private SpecialService specialService;

    @Autowired
    private MemberService memberService;

    @Autowired
    private ProductSpecialService productSpecialService;

    @Autowired
    private ProductService productService;

    @Autowired
    private GroupActivityService groupActivityService;


    /**
     * 查询专区列表
     *
     * @return
     */
    @PostMapping(value = "/list")
    public CommonResult list(@RequestBody SpecialRequest specialRequest) {
        CommonPage<SpecialResp> pageList = specialService.list(specialRequest);
        return CommonResult.success(pageList);
    }

    /**
     * 删除专区(逻辑删除)
     * @param specialRequest
     * @return
     */
    @PostMapping(value = "/deleteSpecial")
    public CommonResult deleteSpecial(@RequestBody SpecialRequest specialRequest) {
        int count = specialService.deleteSpecial(specialRequest.getId());
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 添加专区信息
     *
     * @param special
     * @return
     */
    @PostMapping(value = "/create")
    @OperLog(operModule = "专区管理模块", operDesc = "添加专区信息", operType = SysLogConstants.OPERATION_ADD)
    public CommonResult create(@RequestBody SpecialRequest special) {
        if (1 == special.getCategoryType()) {
            special.setCategoryListStr(special.getOneCategoryIds());
        } else if (2 == special.getCategoryType()) {
            special.setCategoryListStr(List2String(special.getCategoryList()));
        } else {
            return CommonResult.failed("类目类型不能为空");
        }
        int count = specialService.create(special);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 编辑专区信息
     *
     * @param special
     * @return
     */
    @PostMapping(value = "/update")
    @OperLog(operModule = "专区管理模块", operDesc = "编辑专区信息", operType = SysLogConstants.OPERATION_UPDATE)
    public CommonResult update(@RequestBody SpecialRequest special) {
        if (1 == special.getCategoryType()) {
            special.setCategoryListStr(special.getOneCategoryIds());
        } else if (2 == special.getCategoryType()) {
            special.setCategoryListStr(List2String(special.getCategoryList()));
        }
        int count = specialService.update(special);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 把集合转为string类型
     *
     * @param categoryList
     * @return
     */
    public String List2String(List<List<Integer>> categoryList) {
        Set<Integer> set = new HashSet();
        for (List<Integer> c : categoryList) {
            for (Integer integer : c) {
                set.add(integer);
            }
        }
        StringBuilder result = new StringBuilder();
        boolean flag = false;
        for (Integer integer : set) {
            if (flag) {
                result.append(",");
            } else {
                flag = true;
            }
            result.append(integer);
        }
        String s = result.toString();
        return s;
    }

    /**
     * 根据id查询专区列表
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/getSpecialById")
    public CommonResult<Special> getSpecialById(@RequestParam Integer id) {
        Special special = specialService.getSpecialById(id);
        return CommonResult.success(special);
    }

    /**
     * 专区表上架/下架
     *
     * @param id
     * @return
     */
    @PostMapping(value = "/updateStatus")
    @OperLog(operModule = "专区管理模块", operDesc = "专区表上架/下架", operType = SysLogConstants.OPERATION_UPDATE)
    public CommonResult updateStatus(@RequestParam Integer id) {
        int count = specialService.updateStatus(id);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    //---------------------------------------------------- 专区商品 ------------------------------------------------------------------------------

    /**
     * 根据专区id查询商品列表
     *
     * @param specialProListRequest
     * @return
     */
    @PostMapping(value = "/getProductListById")
    public CommonResult getProductListById(@RequestBody SpecialProListRequest specialProListRequest) {
        CommonPage<SpecialProductResponse> pageList = specialService.getProductListById(specialProListRequest);
        return CommonResult.success(pageList);
    }

    /**
     * 专区商品是否推荐
     *
     * @param productSpecial
     * @return
     */
    @PostMapping("/isRecommendFlag")
    public CommonResult isRecommendFlag(@RequestBody ProductSpecial productSpecial) {
        int count = specialService.isRecommendFlag(productSpecial.getProductId(), productSpecial.getSpecialId(), productSpecial.getRecommendFlag());
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 根据专区id查询推荐列表
     *
     * @param specialRecomListRequest
     * @return
     */
    @PostMapping(value = "/getRecommendListById")
    public CommonResult getRecommendListById(@RequestBody SpecialRecomListRequest specialRecomListRequest) {
        CommonPage<SpecialRecommendResponse> pageList = specialService.getRecommendListById(specialRecomListRequest);
        return CommonResult.success(pageList);
    }

    /**
     * 查询改供应商所有商品列表
     *
     * @return
     */
    @GetMapping(value = "/getProducts")
    public CommonResult getProducts() {
        Banker banker = memberService.getBanker();
        if (banker == null) {
            CommonResult.failed("没有该用户！");
        }
        List<Product> products = specialService.getProducts(banker.getId());
        return CommonResult.success(products);
    }

    /**
     * 新增专区商品
     *
     * @param productSpecial
     * @return
     */
    @PostMapping(value = "/addProductSpecial")
    @OperLog(operModule = "专区商品模块", operDesc = "新增专区商品", operType = SysLogConstants.OPERATION_ADD)
    public CommonResult addProductSpecial(@RequestBody ProductSpecialAddRequest productSpecial) {
        if (productService.judgeStatus(productSpecial.getProductId()) == 1) {
            //groupActivityService.judgeState(productSpecial.getProductId()).equals(" ") &&
            return CommonResult.success(specialService.addProductSpecial(productSpecial));
        }
        return CommonResult.failed();
    }

    /**
     * 编辑专区商品
     *
     * @param productSpecial
     * @return
     */
    @PostMapping(value = "/updateProductSpecial")
    @OperLog(operModule = "专区商品模块", operDesc = "编辑专区商品", operType = SysLogConstants.OPERATION_UPDATE)
    public CommonResult updateProductSpecial(@RequestBody ProductSpecial productSpecial) {
        int count = specialService.updateProductSpecial(productSpecial);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 上架/下架 专区商品
     *
     * @param specialId
     * @param productId
     * @return
     */
    @GetMapping(value = "/updateProductSpecialStatus")
    @OperLog(operModule = "专区商品模块", operDesc = "上架/下架专区商品", operType = SysLogConstants.OPERATION_UPDATE)
    public CommonResult updateProductSpecialStatus(@RequestParam("specialId") Integer specialId, @RequestParam("productId") Integer productId) {
        int count = specialService.updateProductSpecialStatus(specialId, productId);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }


    /**
     * 移除 专区商品
     *
     * @param specialId
     * @param productId
     * @return
     */
    @GetMapping(value = "/deleteProductSpecial")
    @OperLog(operModule = "专区商品模块", operDesc = "移除专区商品", operType = SysLogConstants.OPERATION_DELETE)
    public CommonResult deleteProductSpecial(@RequestParam("specialId") Integer specialId, @RequestParam("productId") Integer productId) {
        int count = specialService.deleteProductSpecial(specialId, productId);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    //---------------------------------------------------- 专区动态 ------------------------------------------------------------------------------


    /**
     * 根据专区id查询专区动态
     *
     * @param dynamicSpecialRequest
     * @return
     */
    @PostMapping(value = "/getDynamicsById")
    public CommonResult getDynamicsById(@RequestBody DynamicSpecialRequest dynamicSpecialRequest) {
        CommonPage<DynamicModeResponse> pageList = specialService.getDynamicsById(dynamicSpecialRequest);
        return CommonResult.success(pageList);
    }

    /**
     * 查询所有动态
     *
     * @return
     */
    @GetMapping(value = "/getDynamics")
    public CommonResult getDynamics() {
        List<Dynamic> dynamic = specialService.getDynamics();
        return CommonResult.success(dynamic);
    }

    /**
     * 新增专区动态
     *
     * @param dynamicSpecial
     * @return
     */
    @PostMapping(value = "/addDynamicSpecial")
    @OperLog(operModule = "专区动态模块", operDesc = "新增专区动态", operType = SysLogConstants.OPERATION_ADD)
    public CommonResult addDynamicSpecial(@RequestBody DynamicSpecial dynamicSpecial) {
        int count = specialService.addDynamicSpecial(dynamicSpecial);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 编辑专区动态
     *
     * @param dynamicSpecial
     * @return
     */
    @PostMapping(value = "/updateDynamicSpecial")
    @OperLog(operModule = "专区动态模块", operDesc = "编辑专区动态", operType = SysLogConstants.OPERATION_UPDATE)
    public CommonResult updateDynamicSpecial(@RequestBody DynamicSpecial dynamicSpecial) {
        int count = specialService.updateDynamicSpecial(dynamicSpecial);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 上架/下架 专区动态
     *
     * @param specialId
     * @param dynamicId
     * @return
     */
    @GetMapping(value = "/updateDynamicSpecialStatus")
    @OperLog(operModule = "专区动态模块", operDesc = "上架/下架专区动态", operType = SysLogConstants.OPERATION_UPDATE)
    public CommonResult updateDynamicSpecialStatus(@RequestParam("specialId") Integer specialId, @RequestParam("dynamicId") Integer dynamicId) {
        int count = specialService.updateDynamicSpecialStatus(specialId, dynamicId);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 移除 专区动态
     *
     * @param specialId
     * @param dynamicId
     * @return
     */
    @GetMapping(value = "/deleteDynamicSpecial")
    @OperLog(operModule = "专区动态模块", operDesc = "移除专区动态", operType = SysLogConstants.OPERATION_DELETE)
    public CommonResult deleteDynamicSpecial(@RequestParam("specialId") Integer specialId, @RequestParam("dynamicId") Integer dynamicId) {
        int count = specialService.deleteDynamicSpecial(specialId, dynamicId);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @GetMapping("/getSpecialList")
    public CommonResult getSpecialList() {
        return CommonResult.success(specialService.getSpecialList());
    }

    @PostMapping("/batchProductSpecial")
//    @OperLog(operModule = "商品模块", operDesc = "商品批量添加专区", operType = SysLogConstants.OPERATION_UPDATE)
    @OperationLog(optModule = SysLogConstants.PRODUCT, optType = SysLogConstants.OPERATION_UPDATE, desc = "商品批量添加专区")
    public CommonResult batchProductSpecial(@RequestBody SpecialProductRequest request) {
        productSpecialService.batchProductSpecial(request);
        return CommonResult.success();
    }

    /**
     * 查询动态分类
     *
     * type = APP_DYNAMIC_CATEGORY -app首页动态分类
     */
    @GetMapping("/getDynamicCategory")
    public CommonResult getDynamicCategory(){
        return CommonResult.success(specialService.getDynamicCategory());
    }
}
